﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GatoRatoQueijo
{
    class Cleber : Objeto2D
    {
        private GameWindow window;
        private float anguloMovimento;
        private int tempoMissel;
        private Vector3 destinationPos;

        public Cleber(Texture2D textura, GameWindow window, Nullable<Rectangle> sourceRetangle, float depth)
            : base(textura, sourceRetangle, depth)
        {
            this.window = window;
            ScaleX = 0.35f;
            ScaleY = 0.35f;
            x = y = 600;
            tempoMissel = 0;
        }



        public void update(GameTime gameTime, Vector2 followingPosition)
        {
            validarMovimentacao(followingPosition);
            validarTiros(gameTime, followingPosition);
        }

        public void validarMovimentacao(Vector2 followingPosition)
        {
            destinationPos.X = followingPosition.X;
            destinationPos.Y = followingPosition.Y;

            float dist = (float)(Math.Sqrt((double)(Math.Pow((double)(this.x - destinationPos.X), 2) + Math.Pow((double)(this.y - destinationPos.Y), 2))));
            float vel = 1;

            if (dist < vel)
            {
                this.x = destinationPos.X;
                this.y = destinationPos.Y;
            }
            else
            {
                double angleRadian = Math.Atan2((double)(this.y - destinationPos.Y), (double)(this.x - destinationPos.X));
                double angleDegree = angleRadian * 180 / Math.PI;

                if (angleDegree < 0)
                {
                    angleDegree = angleDegree + 360;
                }

                angleDegree -= 180;

                float angulo = (float)angleDegree;

                this.rotation = angulo;

                anguloMovimento = angulo;

                this.x += (float)(Math.Cos(((angulo) * Math.PI) / 180) * vel);
                this.y += (float)(Math.Sin(((angulo) * Math.PI) / 180) * vel);
            }
        }

        public void validarTiros(GameTime gameTime, Vector2 followingPosition)
        {
            tempoMissel += gameTime.ElapsedGameTime.Milliseconds;

            if (tempoMissel > 3000)
            {
                tempoMissel = 0;


                Missile.CriarMissile(Game1.Self.Content.Load<Texture2D>("ataques"), window, 0.4f, new Vector2(this.x, this.y), new Vector2(followingPosition.X, followingPosition.Y), anguloMovimento);
            }
        }


        public override void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Console.WriteLine(anguloMovimento);

            if ((anguloMovimento > -90) && (anguloMovimento < 90))
            {
                spriteEffects = SpriteEffects.None;
            }
            else
            {
                spriteEffects = SpriteEffects.FlipVertically;
            }


            base.draw(gameTime, spriteBatch);
        }



    }
}
